package com.ItemsRent.IService.service;

import java.util.List;

import com.ItemsRent.IDao.dao.BaseDAO;
import com.ItemsRent.IService.IitemTable;
import com.ItemsRent.model.ItemTable;



public class ItemTableImpl implements IitemTable {
	private BaseDAO<ItemTable> itemTableDao; 
	
	public BaseDAO<ItemTable> getItemTableDao() {
		return itemTableDao;
	}

	public void setItemTableDao(BaseDAO<ItemTable> itemTableDao) {
		this.itemTableDao = itemTableDao;
	}
	
	public void insert(ItemTable itemTable){
		itemTableDao.save(itemTable);  
	}
	
	public void delete(ItemTable itemTable){
		itemTableDao.delete(itemTable);
	}
	
	public void update(ItemTable itemTable){
		itemTableDao.update(itemTable);
	}
	
	/**
	 * 通过物品的ID查找物品
	 *  
	 **/
	/*
	public ItemTable findByItemId(Integer itemId){
		ItemTable ItemTable;
		ItemTable = itemTableDao.find(ItemTable.class, itemId);
		//String hql="from ItemTable item where item.itemId= '"+ itemId + "'";
		//System.out.println("Test");
		//return itemTableDao.find(hql);

		return ItemTable;
	}
	*/
	
	public ItemTable findByItemId(Integer itemId){
		String hql="select item from ItemTable item where item.itemId= "+ "'" + itemId + "'";
		System.out.println("Test");
		List<ItemTable> list = itemTableDao.list(hql);
		for(ItemTable item:list) System.out.println(item.toString());
		return list.get(0);
	}
	
	/**
	 * 返回数据库里的所有的物品
	 * 
	 **/
	public List<ItemTable> findAll(){
		//System.out.println("Line 48 in ItemTableImpl");
		String hql = "from ItemTable";
		List<ItemTable> list = itemTableDao.list(hql);
		return list;
	}
	
	/**
	 * 根据物品的名称查找物品
	 **/
	public List<ItemTable> findByItemName(String ItemName){
		String hql = "from ItemTable item where item.itemName= "+ "'" + ItemName + "'";											//hql------
		List<ItemTable> list=itemTableDao.list(hql);
		return list;
	}
	
	/**
	 * 根据物品种类的ID查找物品
	 **/
	public List<ItemTable> findByItemTypeId(Integer itemTypeId){
		String hql = "from ItemTable item where item.itemType.typeId= " + "'" + itemTypeId + "'";											//hql------
		List<ItemTable> list=itemTableDao.list(hql);
		return list;
	}
	
	/**
	 * 根据物品种类的名字查找物品(上架物品)
	 **/
	public List<ItemTable> findByItemTypeName(String typeName){
		String hql1= "from ItemTable item where item.itemType.typeName= " + "'" + typeName + "'";
		String hql2= "and item.itemStatus= 2";
		List<ItemTable> list=itemTableDao.list(hql1+hql2);
		return list;
	}
	
	/**
	 * 根据用户的ID查找物品
	 */
	public List<ItemTable> findByCustomerId(Integer CustomerId){
		String hql = "from ItemTable item where item.customer.customerId= "+ CustomerId;											//hql------
		List<ItemTable> list=itemTableDao.list(hql);
		return list;
	}
	
	/**
	 * 根据特定用户的ID检查其物品是否上架查找物品
	 */
	@Override
	public List<ItemTable> findByOnlineStatus(Integer CustomerId) {
		String hql="from ItemTable item where item.itemStatus= 2 and item.customer.customerId= " + CustomerId;
		List<ItemTable> list = itemTableDao.list(hql);
		return list;
	}
	
	
	/**
	 * 根据物品是否上架查找物品 
	 */
	public List<ItemTable> findAllOnLineGood(){
		String hql="from ItemTable item where item.itemStatus = 2";
		List<ItemTable> list = itemTableDao.list(hql);
		return list;
	}

	@Override
	public List<ItemTable> findOnlineGoodsByKeyword(String keyword) {
		String hql1="from ItemTable item where item.itemStatus = 2";
		String hql2="item.itemName like " + "'%" + keyword + "%'";
		String hql3="item.itemType.typeName like " + "'%" + keyword + "%'";
		String hql4="item.itemDiscription like " + "'%" + keyword + "%'";
		String hql5=""; //"order by addDate desc";
		String hql = hql1 + "and" + "( " + hql2 + " or " + hql3 + "or " + hql4 + " ) " + hql5;
		List<ItemTable> list = itemTableDao.list(hql);
		return list;
	}

	
}
